package com.example.psychologicalappointmentsystem.Controller.transform;

import com.example.psychologicalappointmentsystem.Entity.ConsultationExtentDO;
import com.example.psychologicalappointmentsystem.Entity.OrderDO;
import com.example.psychologicalappointmentsystem.Entity.User;
import com.example.psychologicalappointmentsystem.JpaRepository.ConsultationExtentRepository;
import com.example.psychologicalappointmentsystem.JpaRepository.UserRepository;
import com.example.psychologicalappointmentsystem.Object.OrderVO;
import com.example.psychologicalappointmentsystem.Object.UserLoginRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName DOToVo
 * Description TODO
 * Author feller
 * Date 2025/2/10 14:40
 * Version 1.0
 */
@Slf4j
@Service
public class DOToVo {

    @Autowired
    ConsultationExtentRepository consultationExtentRepository;

    @Autowired
    UserRepository userRepository;

    public UserLoginRequest userDo_to_userVo(User userDO){
        UserLoginRequest userVO = new UserLoginRequest();
        BeanUtils.copyProperties(userDO,userVO);
        //清空密码
        userVO.setPassword("");

        //扩展字段
        ConsultationExtentDO consultationExtentDO = consultationExtentRepository.findByConsultantId(userDO.getId());
        // 检查 consultationExtentDO 是否为 null
        if (consultationExtentDO != null) {
            userVO.setGoodAt(StringUtils.defaultIfEmpty(consultationExtentDO.getGoodAt(),""));
            userVO.setIndividual(StringUtils.defaultIfEmpty(consultationExtentDO.getIndividual(),""));
            userVO.setWage(StringUtils.defaultIfEmpty(consultationExtentDO.getWage(),""));
            userVO.setTencentMeetingCode(StringUtils.defaultIfEmpty(consultationExtentDO.getTencentmeetingcode(),""));
            userVO.setAliPayCode(StringUtils.defaultIfEmpty(consultationExtentDO.getAlipaycode(),""));
        } else {
            userVO.setIndividual("");
            userVO.setGoodAt("");
            userVO.setWage("");
            userVO.setTencentMeetingCode("");
            userVO.setAliPayCode("");
        }


        return userVO;
    }


    public OrderVO orderDo_to_orderVo(OrderDO orderDO){
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orderDO,orderVO);

        orderVO.setOrderType(orderDO.getOrderType().name());

        User userDO = userRepository.getById(orderDO.getUserId());
        orderVO.setNickName(userDO.getNickname());

        return orderVO;

    }

    /**
     * 更新给定 Page 对象的内容。
     *
     * @param originalPage 原始的 Page 对象
     * @param <R> 更新后的内容项类型
     * @return 包含更新后内容的新 Page 对象
     */
    public static <T, R> Page<R> updatePageContent(Page<T> originalPage,List<R> contentUpdater) {

        // 使用修改后的 content 和原始 page 的其他信息创建一个新的 Page 实例
        return new PageImpl<>(
                contentUpdater,
                PageRequest.of(
                        originalPage.getNumber(),
                        originalPage.getSize(),
                        originalPage.getSort()
                ),
                originalPage.getTotalElements()
        );
    }
}
